//
//  TCAudienceBarrgeView.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/7/3.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation


class TCAudienceBarrgeView: NSView {
    
    var sendAction:ResultString?
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    convenience init(sendAction:ResultString?) {
        self.init()
        self.sendAction = sendAction
        self.setupUI()
    }
    
    func setupUI() {
        self.wantsLayer = true
        self.layer?.backgroundColor = color_gray(0).withAlphaComponent(0.6).cgColor
        
        self.addSubview(sendBtn)
        sendBtn.snp.makeConstraints { (make) in
            make.width.equalTo(40)
            make.centerY.equalToSuperview()
            make.height.equalTo(30)
            make.right.equalTo(-20)
        }
        self.addSubview(textField)
        textField.snp.makeConstraints { (make) in
            make.left.equalTo(20)
            make.right.equalTo(sendBtn.snp.left)
            make.centerY.equalToSuperview()
            make.height.equalTo(18)
        }
    }
    
    lazy var textField: NSTextField = {
        let tx = NSTextField()
        tx.isBordered = false
        tx.textColor = color_gray(255)
        let string = NSAttributedString.init(string: "说点什么", attributes: [
            NSAttributedString.Key.foregroundColor:color_gray(160)
        ])
        tx.placeholderAttributedString = string
        tx.font = NSFont.systemFont(ofSize: 16)
        tx.backgroundColor = NSColor.clear
        tx.maximumNumberOfLines = 1
        tx.focusRingType = .none
        tx.delegate = self
        return tx
    }()
    
    lazy var sendBtn: ZHButton = {
        let btn = ZHButton.init()
        btn.zh_title = "发送"
        btn.zh_font = NSFont.systemFont(ofSize: 14)
        btn.zh_color = color_gray(200)
        btn.target = self
        btn.action = #selector(sendBtnAction(senber:))
        return btn
    }()
    
    @objc func sendBtnAction(senber:ZHButton) {
        if sendAction != nil {
            self.sendAction!(textField.stringValue)
            self.textField.stringValue = ""
        }
    }
}

extension TCAudienceBarrgeView:NSTextFieldDelegate {
    func controlTextDidChange(_ obj: Notification) {
        let textField = obj.object as? NSTextField
        let stringValue = textField?.stringValue ?? ""
        let emojiString = stringValue.disable_emoji()
        if emojiString.count > BARRAGE_MAX_LENGTH {
            textField?.stringValue = emojiString.substringk(toIndex: BARRAGE_MAX_LENGTH)
        }
    }
    
    
}

extension String {
    func disable_emoji() -> String {
        let pattern = "[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]"
       let regex = try? NSRegularExpression.init(pattern: pattern, options: NSRegularExpression.Options.caseInsensitive)
       let modifiedString = regex?.stringByReplacingMatches(in: self, options: [], range: NSRange.init(location: 0, length: self.count), withTemplate: "")
        return modifiedString ?? self
    }
}

